package com.nf;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Hello world!
 *
 */
public class App {
    public static void main( String[] args ) throws Exception{
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取数据库连接，只用了2行代码
        ComboPooledDataSource comboPooledDataSource = (ComboPooledDataSource)ac.getBean("xxxx");


//      测试时间：
        long start_time = System.currentTimeMillis();
        Connection connection = comboPooledDataSource.getConnection();
        Connection connection2 = comboPooledDataSource.getConnection();
        Connection connection3 = comboPooledDataSource.getConnection();
        Connection connection4 = comboPooledDataSource.getConnection();
        long end_time = System.currentTimeMillis();

        System.out.println("耗时："+ (end_time-start_time) +"毫秒");

//      读取mysql数据：
        PreparedStatement ps = connection.prepareStatement("select * from test");
        ResultSet rs = ps.executeQuery();
        while (rs.next()){
            System.out.println(  rs.getString("id") );
            System.out.println(  rs.getString("name") );
        }
        rs.close();
        ps.close();

        System.out.println(connection);


        connection.close();


    }
}
